package org.example.volunteersystem.service.impl;

import org.example.volunteersystem.Mapper.UserMapper;
import org.example.volunteersystem.entity.User;
import org.example.volunteersystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }
    @Override
    public int register(User user) {
        // 先查重
        int count = userMapper.countByUsername(user.getUsername());
        if (count > 0) {
            return -1; // 表示用户名已存在
        }

        // 不存在再插入
        return userMapper.insert(user);
    }
}
